package com.dw.provider;

import com.dw.common.bean.ResourceBean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Sender {
    private final ResourceBean resource;
    private final RandomAccessFile accessFile;

    public Sender(ResourceBean resource) throws FileNotFoundException {
        this.resource = resource;
        File file = new File(Provider.getResourcePath(resource));
        accessFile = new RandomAccessFile(file, "r");
    }

    public byte[] getSendBytes() throws IOException {
        byte[] response = new byte[Math.toIntExact(resource.getLength())];
        accessFile.read(response, Math.toIntExact(resource.getOffset()), Math.toIntExact(resource.getLength()));
        return response;
    }
}
